<?php
//SANITIZE ALL ESCAPES
$sanitize_all_escapes = true;

//STOP FAKE REGISTER GLOBALS
$fake_register_globals = false;

//continue session
if (!isset($_REQUEST['medico'])) 
    session_start();
//
//landing page definition -- where to go if something goes wrong
$landingpage = "index.php?site=" . $_SESSION['site_id'];
//
// kick out if patient not authenticated
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    session_destroy();
    header('Location: ' . $landingpage . '&w');
    exit;
}

$ignoreAuth = true;

include_once('../interface/globals.php');
include_once("$srcdir/pid.inc");
include_once("$srcdir/encounter.inc");
require_once("$srcdir/forms.inc");
require_once("$srcdir/calendar.inc");
require_once("$srcdir/acl.inc");
require_once("$srcdir/formatting.inc.php");
require_once("$srcdir/patient.inc");
require_once("$srcdir/amc.php");

if (isset($_GET["set_encounter"])) {
    setencounter($_GET["set_encounter"]);
}
?>
<html>

    <head>        
        <link rel="stylesheet" href="css/nightingale.css" type="text/css"/>
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-1.10.0.min.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/common.js"></script>
        <script type="text/javascript" src="js/expand.js"></script>
        <script language="JavaScript">
            $.fn.expandAll.defaults.state = "shown";
            $.fn.expandAll.defaults.initTxt = "hide";
            $(function() {
                $("h2.expand").toggler({method: "toggle", speed: 0});
                $("#encounter_forms").expandAll({trigger: "h2.expand", ref: "h2.expand", speed: 0});

            });
        </script>
        <style type="text/css">
            /* --------
              The CSS rules offered here are just an example, you may use them as a base. 
              Shape your 'expand/collapse' content so that it meets the style of your site. 
             --------- */
            * {margin:0; padding:0}
            /* --- Page Structure  --- */
            html {height:500px}
            body {
                min-width:400px;
                width:100%;
                height:500px;
                text-align:center
            }

            .collapse p {padding:0 10px 1em}

            #switch, .switch {margin-bottom:5px; text-align:right}

            /* --- Headings  --- */
            h1 {
                margin-bottom:.75em; 
                font-family:georgia,'times new roman',times,serif; 
                font-size:2.5em; 
                font-weight:normal; 
                color:#c30
            }
            h2{font-size:1em}

            .expand{padding-bottom:.75em}

            /* --- Links  --- */
            a:link, a:visited {
                border: 1px dotted #CCCCCC;
                color: #A2A2A2;
                text-decoration: none !important;
                /*color:blue*/
            }
            a:hover, a:active, a:focus {
                border-style:solid;
                background-color:#f0f0f0;
                outline:0 none;
                text-decoration:none;
            }
            a:active, a:focus {
                text-decoration:none;
                /*color:red;*/
            }
            .expand a {
                display:block;
                padding:3px 10px
            }
            .expand a:link, .expand a:visited {
                border-width:1px;
                background-image:url(images/arrow-down.gif);
                background-repeat:no-repeat;
                background-position:98% 50%;
            }
            .expand a:hover, .expand a:active, .expand a:focus {
                text-decoration:none;
            }
            .expand a.open:link, .expand a.open:visited {
                border-style:solid;
                background:#eee url(images/arrow-up.gif) no-repeat 98% 50%
            }
        </style>
        <style type="text/css">
            div.tab {
                min-height: 0px;
                padding:12px;
            }

            div.form_header_controls {
                float:left;margin-bottom:2px;
            }

            div.form_header {
                float:left;
                margin-left:6px;
            }
        </style>
    </head>

    <body class="body_top">

        <div id="encounter_forms" class="borde_normal">


            <?php
//echo $encounter;
            $dateres = getEncounterDateByEncounter($encounter);
            $encounter_date = date("Y-m-d", strtotime($dateres["date"]));
            $providerIDres = getProviderIdOfEncounter($encounter);
            $providerNameRes = getProviderName($providerIDres);
            ?>

            <div style='float:left width: 100%;'>
                <span class="title"><?php echo "Fecha: " . oeFormatShortDate($encounter_date) ?> </span>
                <?php
                $auth_notes_a = acl_check('encounters', 'notes_a');
                $auth_notes = acl_check('encounters', 'notes');
                $auth_relaxed = acl_check('encounters', 'relaxed');

                if (is_numeric($pid)) {
                    // Check for no access to the patient's squad.
                    $result = getPatientData($pid, "fname,lname,squad");
                    // echo htmlspecialchars( xl('for','',' ',' ') . $result['fname'] . " " . $result['lname'] );
                    if ($result['squad'] && !acl_check('squads', $result['squad'])) {
                        $auth_notes_a = $auth_notes = $auth_relaxed = 0;
                    }
                    // Check for no access to the encounter's sensitivity level.
                    $result = sqlQuery("SELECT sensitivity FROM form_encounter WHERE " .
                            "pid = '$pid' AND encounter = '$encounter' LIMIT 1");
                    if ($result['sensitivity'] && !acl_check('sensitivities', $result['sensitivity'])) {
                        $auth_notes_a = $auth_notes = $auth_relaxed = 0;
                    }
                }
                ?>
            </div>
            <br/>
            <?php
            if ($result = getFormByEncounter($pid, $encounter, "id, date, form_id, form_name, formdir, user, deleted")) {
                echo "<table width='100%' id='partable'>";
                $divnos = 1;
                foreach ($result as $iter) {
                    /* echo "<pre>";
                      print_r($iter);
                      echo "</pre>"; */
                    $formdir = $iter['formdir'];

                    // skip forms whose 'deleted' flag is set to 1
                    if ($iter['deleted'] == 1)
                        continue;

                    // $form_info = getFormInfoById($iter['id']);
                    if (strtolower(substr($iter['form_name'], 0, 5)) == 'camos') {
                        //CAMOS generates links from report.php and these links should
                        //be clickable without causing view.php to come up unexpectedly.
                        //I feel that the JQuery code in this file leading to a click
                        //on the report.php content to bring up view.php steps on a
                        //form's autonomy to generate it's own html content in it's report
                        //but until any other form has a problem with this, I will just
                        //make an exception here for CAMOS and allow it to carry out this
                        //functionality for all other forms.  --Mark
                        echo '<tr title="' . xl('Edit form') . '" ' .
                        'id="' . $formdir . '~' . $iter['form_id'] . '">';
                    } else {
                        echo '<tr title="' . xl('Edit form') . '" ' .
                        'id="' . $formdir . '~' . $iter['form_id'] . '" class="text onerow">';
                    }
                    $user = getNameFromUsername($iter['user']);

                    $form_name = ($formdir == 'newpatient') ? xl('Patient Encounter') : xl_form_title($iter['form_name']);

                    echo "<tr>";
                    echo "<td>";
                    // a link to edit the form
                    echo "<div class='form_header_controls' style='width:100%'>";

                    echo "<div class='form_header' style='width:98%'>";

                    echo "<h2 class='expand'>";
                    // Figure out the correct author (encounter authors are the '$providerNameRes', while other
                    // form authors are the '$user['fname'] . "  " . $user['lname']').
                    if ($formdir == 'newpatient') {
                        $form_author = $providerNameRes;
                    } else {
                        $form_author = $user['fname'] . "  " . $user['lname'];
                    }
                    echo "<b>$form_name</b> <span class='text'  style='font-size: 12px; color: rgb(102, 102, 102);'>by " . htmlspecialchars($form_author) . "</span>";
                    echo "</h2>";
//                    echo "<div class='collapse'>hola</div>";
                    echo "<div class='collapse shown tab borde_normal' id='divid_$divnos' style='display:block'>";

                    if (substr($formdir, 0, 3) == 'LBF') {
                        include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
                        call_user_func("lbf_report", $pid, $encounter, 2, $iter['form_id'], $formdir);
                    } else {

                        include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
                        call_user_func($formdir . "_report", $pid, $encounter, 2, $iter['form_id']);
                    }
                    echo "</div></div></td></tr>";
                    $divnos = $divnos + 1;
                }
                echo "</table>";
            }
            ?>

        </div> <!-- end large encounter_forms DIV -->
        <div class="personPopupResult">
            <frameset cols="*">
                <frame src="forms.php" name="Forms" scrolling="auto">
            </frameset>
        </div>
    </body>

</html>
